#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File        :   spider.py
@Contact     :   18574945291@163.com
@License     :   (C)Copyright 2018-20xx, Liugroup-NLPR-CASIA
@Modify Time :   2020/12/10 15:22
@Author      :   code_wizard 1978992154@qq.com
"""

import sys
import datetime
import requests
import configparser
import pymysql
from selenium import webdriver
import os
sys.path.append(os.getcwd())
import settings


WIN_CHROME_DRIVER_PATH = "D:\mysoft\chromedriver_66.181.exe"
LINUX_CHROME_DRIVER_PATH = '/soft_packge/chromedriver'
LOGIN_URL = "https://mloan.dashuf.com/tsapw/#/partnersLogin"
ENV = sys.platform
_NOW = datetime.datetime.now()
FIRST_URL = "https://mloan.dashuf.com/tsapw/api/site/partners/orders?userName=&companyName=&startDate=&endDate=&pageNum=1&pageSize=10000"


# 抓取小微快贷
class SpiderXWKD:
    def __init__(self):
        self.connect = None  # 数据库连接对象
        self.cursor = None  # 游标对象

    # 连接数据库对象 获取游标
    def connect_db(self):
        self.connect = pymysql.connect(**settings.mysql_dev)  # 开发环境
        # self.connect = pymysql.connect(**settings.mysql_prod)   # 生产环境
        self.cursor = self.connect.cursor(cursor=pymysql.cursors.DictCursor)

    def get_driver_path(self):
        """
        获取当前系统环境 根据环境返回 chrome driver 的文件路径
        """
        if "win32" in ENV.strip().lower():
            return WIN_CHROME_DRIVER_PATH
        else:
            return LINUX_CHROME_DRIVER_PATH

    def format_cookies(self, result):
        """
        格式化cookies
        """
        temp_list = []
        for i in result:
            tem = i['name'] + '=' + i['value']
            temp_list.append(tem)
        return ";".join(temp_list)

    def get_cookie_token(self, username, password):
        """
        获取当前请求的cookie和token
        """
        result = dict()
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--headless')
        browser_path = self.get_driver_path()
        browser = webdriver.Chrome(browser_path, chrome_options=chrome_options)
        browser.get(LOGIN_URL)
        browser.find_element_by_xpath('//input[@type="text"]').clear()
        browser.find_element_by_xpath('//input[@type="text"]').send_keys(username)
        browser.find_element_by_xpath('//input[@type="password"]').clear()
        browser.find_element_by_xpath('//input[@type="password"]').send_keys(password)
        browser.find_element_by_xpath('//button[@class="el-button el-button--primary"]').click()
        cookie_items = browser.get_cookies()  # 获取cookies
        if cookie_items is None:
            return ""
        result["cookie_str"] = self.format_cookies(cookie_items)
        result["token_str"] = browser.execute_script("return localStorage.getItem('DASHU-PARTNER-TOKEN')")
        browser.quit()
        return result

    def parse(self):
        self.connect_db()
        data = self.get_cookie_token(settings.USER_NAME, settings.PASS_WORD)
        headers = {
            'Accept': 'application/json, text/plain, */*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh,en;q=0.9,zh-CN;q=0.8',
            'Connection': 'keep-alive',
            'Cookie': data.get("cookie_str", None),
            'Host': 'mloan.dashuf.com',
            'Referer': 'https://mloan.dashuf.com/tsapw/',
            'token': data.get("token_str", None),
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'
        }
        response = requests.get(url=FIRST_URL, headers=headers)
        if response.status_code == 200 and response.json().get("success", None):
            resp_data = response.json().get("data", None)
            for i in resp_data.get("records", None):
                company_name = i.get("companyName", None)
                self.cursor.execute("select id from chsell_ext_order_xwkd where order_id = %s",
                                    i.get("applicationNum", None))
                exists = self.cursor.fetchone()
                if not exists:
                    self.cursor.execute(
                        "insert into chsell_ext_order_xwkd (order_id,user_name,company_name,product_name,order_created_on,order_status,"
                        "can_loan_amt,loan_amt,channel_sub_name,created_on,update_on,company_name_no_encrypt)values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                        (
                            i.get("applicationNum", None), i.get("userName", None), company_name,
                            i.get("productName", None), i.get("createdDate", None),
                            i.get("applicationStatus", None), i.get("canLoanAmt", None),
                            i.get("loanAmt", None), i.get("channelSubName", None), _NOW, _NOW,
                            company_name.replace("*", "")
                        ))
                    print("insert db success ! {id}".format(id=i.get("applicationNum")))
                else:
                    print("the data is already in database {id}".format(id=i.get("applicationNum")))
                    self.cursor.execute(
                        "update chsell_ext_order_xwkd set order_status=%s,can_loan_amt=%s,loan_amt=%s where order_id = %s",
                        (i.get("applicationStatus", None),
                         i.get("canLoanAmt", None), i.get("loanAmt", None), i.get("applicationNum", None)))

    def __del__(self):
        # 类执行析构函数的时候 释放相关资源
        if self.connect is not None:
            self.connect.commit()
            self.connect.close()
            print("commit成功 析构函数执行成功")


if __name__ == '__main__':
    spider = SpiderXWKD()
    spider.parse()
